package com.myapplication.app.fragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.*;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.myapplication.app.HomeActivity;
import com.myapplication.app.MessageImageActivity;
import com.myapplication.app.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;

/**
 * Created by tukela on 2015/3/3.
 */
public class FragmentChat extends Fragment implements View.OnClickListener {
    private View rootView;
    private ImageView imageShow;
    private ImageView imagePhoto;
    private ImageView imageCamera;
    private MenuItem imgBar;
    private MenuItem imgPhoto;
    private MenuItem imgCamera;
    private MenuItem imgedit;
    private TextView tv_Msg;
    private TextView tv_Me;
    private TextView tv_Bus;
    private int PHOTOCODE = 0x123;
    private int CAMERACODE = 0x234;
    private HomeActivity activity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity= (HomeActivity) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_chat, null);
        imageShow = (ImageView)rootView. findViewById(R.id.show);
//        imagePhoto = (ImageView)rootView.  findViewById(R.id.photo);
//        imageCamera = (ImageView)rootView.  findViewById(R.id.camera);
        tv_Msg = (TextView)rootView.  findViewById(R.id.msg);
        tv_Me = (TextView)rootView.  findViewById(R.id.me);
        tv_Bus = (TextView) rootView. findViewById(R.id.bus);
        imageShow.setOnClickListener(this);
//        imagePhoto.setOnClickListener(this);
//        imageCamera.setOnClickListener(this);
        tv_Msg.setOnClickListener(this);
        tv_Me .setOnClickListener(this);
        tv_Bus.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.menu_home, menu);
            imgPhoto=menu.findItem(R.id.action_photo);
            imgCamera=menu.findItem(R.id.action_camera);
            imgedit=menu.findItem(R.id.action_edit);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.action_photo:/*提交图片*/
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, PHOTOCODE);
                break;
            case R.id.action_camera:/*提交图片*/
                Intent intent2 = new Intent("android.media.action.IMAGE_CAPTURE");
                intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "camera.jpg")));
                intent2.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
                startActivityForResult(intent2, CAMERACODE);
                break;
            case R.id.action_edit:/*提交图片*/
                Intent intent=new Intent(activity,MessageImageActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.photo:
//                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//                intent.addCategory(Intent.CATEGORY_OPENABLE);
//                intent.setType("image/*");
//                intent.putExtra("crop", "true");
//                intent.putExtra("aspectX",1);
//                intent.putExtra("aspectY",1);
//                intent.putExtra("outputX", 80);
//                intent.putExtra("outputY", 80);
//                intent.putExtra("return-data",true);
//                startActivityForResult(intent, PHOTOCODE);

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, PHOTOCODE);
                break;
            case R.id.camera:
                Intent intent2 = new Intent("android.media.action.IMAGE_CAPTURE");
                intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "camera.jpg")));
                intent2.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
                startActivityForResult(intent2, CAMERACODE);
                break;
            case R.id.login:
//                checkRight();
//                loadDate();

                break;
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERACODE && resultCode == Activity.RESULT_OK) {
            imageShow.setBackground(Drawable.createFromPath(new File(
                    Environment.getExternalStorageDirectory(), "camera.jpg")
                    .getAbsolutePath()));
//            imageShow.setImageDrawable(Drawable.createFromPath(new File(
//                    Environment.getExternalStorageDirectory(), "camera.jpg")
//                    .getAbsolutePath()));
        } else if (requestCode == PHOTOCODE && resultCode == Activity.RESULT_OK) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            imageShow.setBackground(Drawable.createFromPath(picturePath));
                    //BitmapFactory.decodeFile(picturePath));
//            Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
//            imageShow.setImageDrawable(new BitmapDrawable(cameraBitmap));
        }
    }
}


